﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Indie_Game_Contest
{
    class ObjectManager
    {
        //////////////////////////////////////////////////////////////////////////
        // Data Members
        static ObjectManager instance = new ObjectManager();

        List<CBaseObject> objects = new List<CBaseObject>();
        List<CBaseObject> tiles = new List<CBaseObject>();
        List<Platform> platforms = new List<Platform>();


        //////////////////////////////////////////////////////////////////////////
        // Methods
        ObjectManager() { objects.Capacity = 128; }

        public int AddTile(Tile tile)
        {
            tiles.Add(tile);
            return tiles.Count - 1;
        }

        public Tile GetTile(int nID)
        {
            return (Tile)tiles[nID];
        }

        public void AddPlatform( Platform platform)
        {
            platforms.Add(platform);
        }

        public Platform GetPlatform(int nID )
        {
            return platforms[nID];
        }

        
        public void Update(float dt)
        {
            foreach (CBaseObject obj in objects)
                obj.Update(dt);

            foreach (CBaseObject obj in tiles)
                obj.Update(dt);

            foreach (Platform obj in platforms)
                obj.UpdatePlatform(dt);


        }
        public void Render()
        {
            foreach (CBaseObject obj in objects)
                obj.Render();

            foreach (CBaseObject obj in tiles)
                obj.Render();
        }

        public void CheckCollisions()
        {
            foreach (CBaseObject obj in objects)
            {
                if (obj.Collision)
                {
                    for (int i = 0; i < objects.Count; ++i)
                    {
                        if (obj != objects[i] && objects[i].Collision)
                        {
                            obj.CheckCollision(objects[i]);
                        }
                        
                    }
                }
                
            }

            foreach (CBaseObject obj in objects)
            {
                if (obj.Collision)
                {
                    for (int i = 0; i < tiles.Count; ++i)
                    {
                        if (obj != tiles[i] && tiles[i].Collision)
                        {
                            obj.CheckCollision(tiles[i]);
                        }

                    }
                }

            }

            foreach (Platform obj in platforms)
            {
                for (int i = 0; i < obj.IDs.Count; ++i)
                {
                    if (tiles[i].Collision)
                    {
                        if (tiles[i].CheckCollision(GameplayState.Instance.Player) == CollisionSide.Top)
                        {
                            obj.BPlayerOnPlatform = true;
                            break;
                        }
                    }
                }

                if (obj.BPlayerOnPlatform == true)
                    break;
            }


        }

        public void AddObject(CBaseObject obj)
        {
            objects.Add(obj);
        }
        public void RemoveObject(CBaseObject obj)
        {
            objects.Remove(obj);
        }

        public void Dispose()
        {
            objects.Clear();
            tiles.Clear();
            platforms.Clear();
        }

        //////////////////////////////////////////////////////////////////////////
        // Properties
        public List<CBaseObject> Objects
        {
            get { return objects; }
            set { objects = value; }
        }
        public static ObjectManager Instance
        {
            get
            {
                return instance;
            }
        }
    }
}
